package com.magictower.primary.utils;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.PixelFormat;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.magictower.primary.R;
import com.magictower.primary.common.SPConstant;

public class ToastUtils {

    public static void showToast(final Activity ctx, final String text) {
        if ("main".equals(Thread.currentThread().getName())) {
            Toast.makeText(ctx, text, Toast.LENGTH_SHORT).show();
        } else {
            ctx.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(ctx, text, Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

    /**
     * 自定义归属地浮窗 需要权限android.permission.SYSTEM_ALERT_WINDOW
     */
    public static View showWindowToast(final Context ctx, final WindowManager mWM, String text) {
        //获取屏幕高
        final int winWidth = mWM.getDefaultDisplay().getWidth();
        final int winHeight = mWM.getDefaultDisplay().getHeight();

        final WindowManager.LayoutParams params = new WindowManager.LayoutParams();
        params.setTitle("Toast");

        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        params.width = WindowManager.LayoutParams.WRAP_CONTENT;
        params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
                WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
        params.format = PixelFormat.TRANSLUCENT;
        params.type = WindowManager.LayoutParams.TYPE_PHONE;// 电话窗口。它用于电话交互（特别是呼入）。它置于所有应用程序之上，状态栏之下。
        params.gravity = Gravity.TOP | Gravity.START;

        final SharedPreferences mPref = ctx.getSharedPreferences(SPConstant.CONFIG, Context.MODE_PRIVATE);

        int lastX = mPref.getInt(SPConstant.TOAST_X, 0);
        int lastY = mPref.getInt(SPConstant.TOAST_Y, 0);
        // 设置浮窗的位置, 基于左上方的偏移量
        params.x = lastX;
        params.y = lastY;

        final View view = View.inflate(ctx, R.layout.toast_show, null);
        int style = mPref.getInt(SPConstant.TOAST_STYLE, 0);

        view.setBackgroundResource(SPConstant.ITEMS_ID[style]);// 根据存储的样式更新背景
        TextView textView = (TextView) view.findViewById(R.id.tv_address);
        textView.setText(text);

        mWM.addView(view, params);// 将view添加在屏幕上(Window)

        view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int startX = 0;
                int startY = 0;
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        startX = (int) event.getRawX();
                        startY = (int) event.getRawY();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        int endX = (int) event.getRawX();
                        int endY = (int) event.getRawY();

                        //计算移动偏移量
                        int dx = endX - startX;
                        int dy = endY - startY;

                        //更新浮窗位置
                        params.x += dx;
                        params.y += dy;
                        //不能超过的偏移
                        if (params.x < 0) {
                            params.x = 0;
                        }
                        if (params.y < 0) {
                            params.y = 0;
                        }
                        if (params.x > winWidth - view.getWidth()) {
                            params.x = winWidth - view.getWidth();
                        }
                        if (params.y > winHeight - view.getHeight()) {
                            params.y = winHeight - view.getHeight();
                        }
                        mWM.updateViewLayout(view, params);

                        //重新初始化坐标
                        startX = (int) event.getRawX();
                        startY = (int) event.getRawY();
                        break;
                    case MotionEvent.ACTION_UP:
                        SharedPreferences.Editor editor = mPref.edit();
                        editor.putInt(SPConstant.TOAST_X, params.x);
                        editor.putInt(SPConstant.TOAST_Y, params.y);
                        editor.apply();
                        break;
                    default:
                        break;
                }
                return true;
            }
        });
        return view;
    }
}
